package com.pioneer.cloudstorage.pojo.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.ValueConverter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Data
@Document(indexName = "file_content")
public class FileContentIndex {

    @Id
    private String id;

    @Field(type = FieldType.Long)
    private Long fileId;

    @Field(type = FieldType.Long)
    private Long userId;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String fileName;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String content;

    @Field(type = FieldType.Keyword)
    private String fileType;

    @Field(type = FieldType.Keyword)
    private String mimeType;

    @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
    private LocalDateTime indexedTime;

    @Field(type = FieldType.Boolean)
    private Boolean indexedSuccess;

    public FileContentIndex() {
        this.indexedTime = LocalDateTime.now();
        this.indexedSuccess = false;
    }

    public FileContentIndex(Long fileId, Long userId, String fileName, String mimeType) {
        this();
        this.fileId = fileId;
        this.userId = userId;
        this.fileName = fileName;
        this.mimeType = mimeType;
        this.fileType = getFileTypeFromMime(mimeType);
    }

    private String getFileTypeFromMime(String mimeType) {
        if (mimeType == null) return "other";
        if (mimeType.startsWith("text/")) return "text";
        if (mimeType.contains("pdf")) return "pdf";
        if (mimeType.contains("word") || mimeType.contains("document")) return "word";
        if (mimeType.contains("excel") || mimeType.contains("spreadsheet")) return "excel";
        return "other";
    }
}